| 1 |  |  | import { RankingDataTeamObject } from "../Types/Ranking" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import Axios from "axios" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import { setupCache, setup } from "axios-cache-adapter" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import localforage from "localforage" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | export function mapPsdStatus(statusCode: number): string | null { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |   const statusCodes = new Map([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     [0, `Gepland`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     [1, `Forfait`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     [2, `Afgelast`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     [3, `Onderbroken`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   return statusCodes.get(statusCode) || null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | export function mapPsdStatusShort(statusCode: number): string | null { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |   const statusCodes = new Map([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     [0, ``], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     [1, `FF`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     [2, `AFG`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     [3, `STOP`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   return statusCodes.get(statusCode) || null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |   // Rank lager: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |   if (a.rank < b.rank) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |   if (a.rank > b.rank) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |   // Aantal overwinningen hoger: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |   if (a.wins > b.wins) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |   if (a.wins < b.wins) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |   // Doelpuntensaldo beter: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |   if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |   if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |   // Aantal gemaakte doelpunten hoger: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   if (a.goalsScored > b.goalsScored) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |   if (a.goalsScored < b.goalsScored) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   // Aantal uitoverwinningen hoger: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |   if (a.winsAway > b.winsAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |   if (a.winsAway < b.winsAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |   // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |   if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |   if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |   // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |   if (a.goalsScoredAway > b.goalsScoredAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     return -1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |   if (a.goalsScoredAway < b.goalsScoredAway) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     return 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |   return a.team?.club?.localName.localeCompare(b.team?.club?.localName) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | export function replaceAll(source: string, search: string, replacement: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |   return source.replace(new RegExp(search, `g`), replacement) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | export function translateGameResult(result: string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |   const statusCodes = new Map([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     [`WON`, `Gewonnen`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     [`EQUAL`, `Gelijkgespeeld`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     [`LOST`, `Verloren`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |   ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |   return statusCodes.get(result) || null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  * Map a positionCode to a descriptive label. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  * @param {string} positionCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | export function mapPositionCode(positionCode: string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |   return getPositions().get(positionCode) || null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  * List of all positions, in order of position on the fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  * @param {string} positionCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | export function getPositions() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   const positions = new Map([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     [`k`, `Doelman`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     [`d`, `Verdediger`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     [`m`, `Middenvelder`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     [`a`, `Aanvaller`], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |   ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |   return positions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | export const forageStore = localforage.createInstance({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |   driver: [localforage.INDEXEDDB, localforage.LOCALSTORAGE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |   name: `kcvv-cache`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | export const request = setup({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |   cache: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     maxAge: 15 * 60 * 1000, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     exclude: { query: false, filter: (config: { url: string }) => config.url.startsWith(`/profiles`) }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     store: forageStore, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     invalidate: async (config, request) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |       if (request.clearCacheEntry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         // eslint-disable-next-line @typescript-eslint/ban-ts-comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         // @ts-ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         await config.store.removeItem(config.uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     readOnError: (error: { response: { status: number } }, request: any) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |       return !error.response || (error.response.status >= 500 && error.response.status < 600) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     clearOnStale: false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |   }, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | }) | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |